using System;
using System.Collections.Generic;
using System.Web.UI.HtmlControls;
using Microsoft.SharePoint;
using Navigant.Infrastructure.Helpers;
using Navigant.Model.Custodians;
using Navigant.Model.PreservationNotices;

namespace Navigant.Web.UI
{
	public class EventHistoryList : ListControl
    {
        #region Fields

        private Custodian historyCustodian;
        private string historyPageUrlFormatString;
        private string noticeTemplatePageUrlFormatString;

        #endregion

        #region Constructors

        public EventHistoryList(int scrollHeight)
		{
            this.historyCustodian = null;
            this.ContentHeight = scrollHeight;
            this.historyPageUrlFormatString = "{0}/_layouts/NavigantPages/PreservationNoticesHistory.aspx?PreservationNoticeHistoryID={1}";
            this.noticeTemplatePageUrlFormatString = "{0}/Lists/Preservation%20Notices/DispForm.aspx?ID={1}";
		}

        #endregion

        #region SetHistoryCustodian

        public void SetHistoryCustodian(Custodian historyCustodian)
        {
            this.historyCustodian = historyCustodian;
            this.BindHistoryCustodian();
        }

        #endregion

        #region OnInit

        protected override void OnInit(EventArgs e)
		{
			base.OnInit(e);
			try
			{
				this.AddHeaderCell("Action", 10);
				this.AddHeaderCell("Notice", 10);
				this.AddHeaderCell("Date", 10);
			}
			catch (Exception ex)
			{
                Logger.LogError(ex, this.historyCustodian.ParentCase);
			}
		}

        #endregion

        #region BindHistoryCustodian

        private void BindHistoryCustodian()
		{
			SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                try
                {
                    if (this.historyCustodian == null)
                    {
                        this.Title = "Custodian Details";
                        this.DisplayMessage("Click on a status entry to view details for a custodian.");
                    }
                    else
                    {
                        this.Title = string.Format("Details for {0}",
                            historyCustodian.Name.FullName);

                        PreservationNoticeService service = new PreservationNoticeService(this.historyCustodian.ParentCase);
                        IList<PreservationNotice> notices = service.FindBySiteAnd(historyCustodian.Id);

                        if (notices.Count == 0)
                        {
                            this.DisplayMessage("No notices have been sent for this custodian.");
                        }
                        else
                        {
                            foreach (PreservationNotice notice in notices)
                            {
                                this.AddGridRow();

                                HtmlTableCell cell = this.CreateGridCell();

                                HtmlAnchor anchor = new HtmlAnchor();
                                anchor.InnerText = notice.Status.ToString();
                                anchor.HRef = string.Format(this.historyPageUrlFormatString,
                                    historyCustodian.ParentCase.SiteAddress.AbsoluteUri,
                                    notice.Key.ToString());
                                cell.Controls.Add(anchor);

                                if (notice.Id == 0)
                                {
                                    this.AddGridCell(notice.Template.Name);
                                }
                                else
                                {
                                    cell = this.CreateGridCell();

                                    anchor = new HtmlAnchor();
                                    anchor.InnerText = notice.Template.Name;
                                    anchor.HRef = string.Format(this.noticeTemplatePageUrlFormatString,
                                        historyCustodian.ParentCase.SiteAddress.AbsoluteUri,
                                        notice.TemplateId.ToString());
                                    cell.Controls.Add(anchor);
                                }

                                this.AddGridCell(notice.DateMessageCreated.ToShortDateString());
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    Logger.LogError(ex, this.historyCustodian.ParentCase);
                }
            });
        }

        #endregion
    }
}
